<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2012.03.17.
   地区模型($$)*/

!defined('Q_PATH') && exit;

class DistrictModel extends CommonModel{

	static public function init__(){
		return array(
			'table_name'=>'district',
			'check'=>array(
				'district_name'=>array(
					array('require',Q::L('地区名字不能为空','__COMMON_LANG__@Common')),
				),
				'district_sort'=>array(
					array('number',Q::L('序号只能是数字','__COMMON_LANG__@Common'),'condition'=>'notempty','extend'=>'regex'),
				)
			),
		);
	}

	static function F(){
		$arrArgs=func_get_args();
		return ModelMeta::instance(__CLASS__)->findByArgs($arrArgs);
	}

	static function M(){
		return ModelMeta::instance(__CLASS__);
	}

	public function getDistrictByUpid($Upid,$sSort='DESC',$arrWhere=array()){
		$Upid=is_array($Upid)?array_map('intval',(array)$Upid):intval($Upid);

		if($Upid!==null){
			$arrWhere=array_merge(array('district_upid'=>array('in',$Upid)),$arrWhere);
			return self::F()->where($arrWhere)->order("district_sort {$sSort}")->asArray()->getAll();
		}

		return array();
	}

	public function getSiteid($nCityId,$nProvinceId=''){
		if($nProvinceId && $this->isSpecial($nProvinceId)){
			$nCityId=$nProvinceId;
		}
		return SiteModel::F('city_id=?',$nCityId)->getColumn('site_id');
	}
	
	public function getCityid($nCityId=0,$nProvinceId=0){
		if($nProvinceId && $this->isSpecial($nProvinceId)){
			$nCityId=$nProvinceId;
		}else{
			if(!$nCityId){
				$oTemp=self::F('district_upid=?',$nProvinceId)
					->order('district_sort ASC,district_id ASC')
					->getOne();
				$nCityId=$oTemp['district_id'];
			}
		}
		return $nCityId;
	}

	/**
	 * 获取区域
	 */
	public function getDistrict($sType='province',$nId=null,$nIsName=0,$nIsSite=0,$nOpenSite=0,$nOpenSite_=1){
		$arrLevel=array('province'=>1,'city'=>2,'district'=>3,'community'=>4);
		$arrWhere=array();

		switch($sType){
			case 'province':// 省
				$arrWhere['district_level']=1;

				// 省份
				if($nOpenSite==1){
					$arrProvinceId=Model::F_('site')->distinct(true)
						->where('site_status=1')
						->getColumn('province_id',true);
					$arrWhere['district_id']=$arrProvinceId?array('in',$arrProvinceId):0;
				}
				break;
			case 'city':// 市
			case 'district':// 县
			case 'community':// 乡
				if($nIsName==1){
					$nId=self::F('district_level='.($arrLevel[$sType]-1).' AND district_name=?',$nId)->getColumn('district_id');
				}

				// 直辖市或者特别行政区（有部分特殊除外）
				if(($nIsSite==1 || ($nOpenSite==1 && $nOpenSite_==1)) && ($sType=='city' || $sType=='district') && $this->isSpecial($nId)){
					if($sType=='city'){
						$arrWhere['district_id']=$nId;
					}
					if($sType=='district'){
						$arrWhere['district_upid']=$nId;
					}
					$arrWhere['district_level']=$arrLevel[$sType]-1;
				}else{
					// 开通城市
					if($sType=='city' && $nOpenSite==1 && $nOpenSite_==1){
						$arrSiteId=Model::F_('site')->distinct(true)
						->where('site_status=1 AND province_id='.$nId)
						->getColumn('city_id',true);
						$arrWhere['district_id']=$arrSiteId?array('in',$arrSiteId):0;
					}elseif($sType=='city' && $nIsSite==1){// 城市站点(包含县级市)
						$oDb=Db::RUN();
						$sSql="SELECT A.district_id FROM ".Q::C('DB_PREFIX')."district AS A WHERE A.district_upid={$nId} || (A.district_upid IN(
							SELECT B.district_id FROM ".Q::C('DB_PREFIX')."district AS B where B.district_upid={$nId} AND B.district_level=2) AND INSTR(right(A.district_name,1),'市')>0)";
						$arrCityId=$oDb->getAllRows($sSql);
						$arrTemp=array();
						if(is_array($arrCityId)){
							foreach($arrCityId as $arrT){
								$arrTemp[]=$arrT['district_id'];
							}
						}
						$arrWhere['district_id']=$arrTemp?array('in',$arrTemp):0;
					}else{
						$arrWhere['district_upid']=$nId;
						$arrWhere['district_level']=$arrLevel[$sType];
					}
				}
				break;
		}

		return self::F()->setColumns('district_id,district_name')
			->where($arrWhere)
			->order('district_sort ASC,district_id ASC')
			->asArray()
			->getAll();
	}

	public function isSpecial($nId){
		return in_array($nId,array(1,2,9,22,32,33,34,35));
	}

	protected function beforeSave_(){
		$this->district_name=C::text($this->district_name);
		
		if($this->district_sort<0){
			$this->district_sort=0;
		}
		if($this->district_sort>999){
			$this->district_sort=999;
		}
	}

}
